package com.example.skillcinema.ui.screens.search

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.example.skillcinema.R
import com.example.skillcinema.databinding.FragmentYearSearchBinding
import com.example.skillcinema.ui.screens.search.viewModels.SearchViewModel

class YearSearchFragment : Fragment() {
    private var _binding: FragmentYearSearchBinding? = null
    private val binding: FragmentYearSearchBinding
        get() = _binding!!
    private val viewModel: SearchViewModel by activityViewModels<SearchViewModel>()
    private var year: Pair<Int, Int> = 1000 to 3000
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentYearSearchBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (activity as? AppCompatActivity)?.setSupportActionBar(binding.toolbar)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayShowTitleEnabled(false)
        binding.toolbar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }
        binding.submitButton.setOnClickListener {
            saveYear()
        }
    }

    fun saveYear() {
        year = binding.yearFrom.selectedYear to binding.yearTo.selectedYear
        viewModel.year = year
        findNavController().navigateUp()
    }

    companion object {

    }
}